<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    let iparr = [
      '192.168.1.1',
      '192.168.1.2',
      '192.168.1.21',
      '192.168.1.3',
      '192.168.1.42',
      '192.168.1.5'
    ]
    iparr.sort((a, b) => {
      const ip1 = a.split('.').map(el => el.padStart(3, '0')).join('')
      const ip2 = b.split('.').map(el => el.padStart(3, '0')).join('')
      return ip1 - ip2
    })
    console.log(iparr, 'iparr');
  </script>
</body>
</html>